package lagou.user.server.lagouuser.controller;

import lagou.user.server.lagouuser.entity.UserBase;
import lagou.user.server.lagouuser.service.UserBaseService;
import lagou.user.server.lagouuser.service.UserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
@RefreshScope
public class UserController {


    @Value("${lagou.message}")
    private String message;

    @Autowired
    private UserBaseService userBaseService;


    @Autowired
    private UserTokenService userTokenService;


    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        return "hello"+message;
    }



    @RequestMapping("/verifyAndInsert")
    @ResponseBody
    public String verifyAndInsert(String email,String pwd,String verifyCode){

        return userBaseService.verifyAndInsert(email, pwd, verifyCode);
    }



    @RequestMapping("/login")
    @ResponseBody
    public String loginUser(String email, String pwd, HttpServletResponse httpServletResponse){
         UserBase userBase = userBaseService.findUserBase(email, pwd);
         if (null==userBase){
             return "账号密码错误";
         }
         //添加cookie
         String uuid = UUID.randomUUID().toString();
         //添加进入cookie
        Cookie cookie=new Cookie("token",uuid);
        cookie.setDomain("www.test.com");
        cookie.setPath("/");
        httpServletResponse.addCookie(cookie);
        Cookie cookieUser=new Cookie("email",email);
        cookieUser.setDomain("www.test.com");
        cookieUser.setPath("/");
        httpServletResponse.addCookie(cookieUser);
        userTokenService.insertUserToken(email,uuid);
        return "success";
    }

}
